package com.passioneffect.collection {
	import flash.events.EventDispatcher;

	/**
	 * @author Passion Effect
	 */
	public class SimpleList{
		protected var startItem : ListItem;
		protected var endItem : ListItem;
		protected var total : int = 0;

		public function SimpleList() {
		}

		public function addItem(data : *) : void {
			if(total > 0) {
				var newObj : ListItem = new ListItem(data);
				endItem = endItem.insertBehind(newObj);
			}else {
				startItem = endItem = new ListItem(data);
			}
			++total;
		}

		public function iterator() : LinkListIterator {
			return new LinkListIterator(this);
		}

		public function getLength() : int {
			return total;
		}

		public function getStartItem() : ListItem {
			return startItem;
		}

		public function getEndItem() : ListItem {
			return endItem;
		}
		public function getItemAt(index : int) : ListItem {
			if(index >= total)return null;
			
			var item : ListItem;
			var count : int;
			if(index < total / 2) {
				count = 0;
				item = startItem;
				while(count < index) {
					item = item.next;
					++count;
				}
			}else {
				count = total - 1;
				item = endItem;
				while(count > index) {
					item = item.previous;
					--count;
				}
			}
			return item;
		}
		public function toString():String{
			var itr:LinkListIterator=iterator();
			var str:String="";
			while(itr.hasNext()){
				str+=itr.next().getData()+"\r";
			}
			
			return str;
		}
		
	}
}
